package com.lantosec.hmrp.modules.supplier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.supplier.entity.SupplierLinkman;
import com.lantosec.hmrp.modules.supplier.mapper.SupplierLinkmanMapper;
import com.lantosec.hmrp.modules.supplier.service.ISupplierLinkmanService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierLinkmanVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供应商联系人 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-20
 */
@Service
@Primary
@Log4j2
public class SupplierLinkmanServiceImpl extends ServiceImpl<SupplierLinkmanMapper, SupplierLinkman> implements ISupplierLinkmanService {
    @Autowired
    private ISysUploadsService iSysUploadsService;


    /**
     * 供应商联系人查询
     * @param page
     * @param supplierCode
     * @param linkmanName
     * @param linkmanTel
     * @return
     */
    @Override
    public PageVO<SupplierLinkmanVO> querySupplierLinkman(PageVO<SupplierLinkman> page, String supplierCode, String linkmanName, String linkmanTel, Integer salesmanFlag) {
        return baseMapper.querySupplierLinkman(page,supplierCode,linkmanName,linkmanTel,salesmanFlag);
    }

    @Override
    public List<SupplierLinkman> queryAll(String supplierCode) {
        List<SupplierLinkman> list = this.list(new QueryWrapper<SupplierLinkman>().select(SupplierLinkman.ColName.linkman_id.toString(),SupplierLinkman.ColName.linkman_name.toString())
                                                        .eq(SupplierLinkman.ColName.supplier_code.toString(),supplierCode));

        return list;
    }

    @Override
    public Page<SupplierLinkmanVO> getBySupplierCode(Page<SupplierLinkman> page,String supplierCode) {
        this.page(page,new QueryWrapper<>());
        return baseMapper.getBySupplierCode(page,supplierCode);
    }

    @Override
    public SupplierLinkmanVO getSavePath(String linkmanId) {
        SysUploads sysUploads = new SysUploads();
        SupplierLinkmanVO supplierLinkmanVO = new SupplierLinkmanVO();
        try {
            supplierLinkmanVO = this.getById(linkmanId).convertToVO();
            //获得身份证正面存放路径
            if (StringUtils.isNotEmpty(supplierLinkmanVO.getIdcardFront())) {
                sysUploads = iSysUploadsService.getById(supplierLinkmanVO.getIdcardFront());
                supplierLinkmanVO.setIdcardFrontSavePath(InitDataListener.uploadsUrl + sysUploads.getSavePath() + sysUploads.getFileName());
            }
            //获得身份证反面存放路径
            if (StringUtils.isNotEmpty(supplierLinkmanVO.getIdcardBack())) {
                sysUploads = iSysUploadsService.getById(supplierLinkmanVO.getIdcardBack());
                supplierLinkmanVO.setIdcardBackSavePath(InitDataListener.uploadsUrl + sysUploads.getSavePath() + sysUploads.getFileName());
            }
            //获得购销资格证存放路径
            //判断是否是业务员
            if (supplierLinkmanVO.getSalesmanFlag() == 1) {
                if (StringUtils.isNotEmpty(supplierLinkmanVO.getQualification())) {
                    sysUploads = iSysUploadsService.getById(supplierLinkmanVO.getQualification());
                    supplierLinkmanVO.setQualificationkSavePath(InitDataListener.uploadsUrl + sysUploads.getSavePath() + sysUploads.getFileName());

                }
            }
        }catch (Exception e){
    }
        return supplierLinkmanVO;
    }
}
